package frsf.cidisi.exercise.tp.situationCalculus;

import frsf.cidisi.faia.agent.ActionFactory;
import frsf.cidisi.faia.agent.Perception;
import frsf.cidisi.faia.agent.situationcalculus.KnowledgeBase;
import frsf.cidisi.faia.agent.situationcalculus.SituationCalculusPerception;
import frsf.cidisi.faia.exceptions.PrologConnectorException;

import java.util.Hashtable;

import componentes.Aldea;
import componentes.Mapa;

public class Indiana_State extends KnowledgeBase {
	private Aldea posicion;
	private int valor_Acumulado;
	private double pesoCofre_Agente;
	private Mapa mapa;
	
	 /**
     * Here we called the superclass constructor with the name of the file
     * which contains the Prolog sentences. Then we setup the agent state.
     * @throws PrologConnectorException
     */
    public Indiana_State() throws PrologConnectorException {

		//TODO: Replace file name  
        super("ArchivoProlog/indianaJones.pl");

        this.mapa= new Mapa();
        this.mapa.inicializar_mapa();
    	this.posicion = mapa.getInicial();
    	this.pesoCofre_Agente = 0;
    	this.valor_Acumulado=0;

        this.initState();
    }

    /**
     * This method receives a Perception object and updates the agent state.
     * It is called by the 'see(Perception)' method in the PacmanLogicAgent
     * class.
     * @param perception
     */
    @Override
    public void updateState(Perception p) {
        this.tell((SituationCalculusPerception) p);
    }
    
    
    /**
     * Prints a string representation of the knowledge base.
     * @return
     */
    @Override
    public String toString() {  
        String str = "";

        // Orientation
        str = str + "Posicion: (" + posicion.getNombre()+")\n";
        str = str + "Valor Acumulado: " + this.valor_Acumulado+"\n";
        str = str + "Peso Acumulado: " + this.pesoCofre_Agente;

        return str;
    }
    
    
    /**
     * Aca se indica cual es el objeto CalculusActionFactory definido
     * por el usuario. 
     */
    /**
     * Here we return the ActionFactory defined by the user. In this case
     * we return a Indiana_JonesActionFactory instance.
     * @return
     */
    
    @Override
    public ActionFactory getActionFactory() {
        return Indiana_JonesActionFactory.getInstance();
    }

    
    
    /**
     * This method must return the string used in the Prolog file to represent
     * the best action sentence. In this example it is used like this:
     *
     *      bestAction(noAction,S):-goalReached(S),!.
     *
     * The knowledge base needs to know how we named that sentence.
     */
    @Override
    public String getBestActionPredicate() {
        return "bestAction";
    }

   
    /**
     * Similar to 'getBestActionPredicate'.
     */
    @Override
    public String getExecutedActionPredicate() {
        return "action";
    }

    /**
     * Similar to 'getBestActionPredicate'.
     */
    @Override
    public String getSituationPredicate() {
        return "actualSituation";
    }


    /**
     * Here we setup the initial state of the agent.
     */
	@Override
	public void initState() { 
		
		this.addKnowledge("position(aragaey,0)");
		this.addKnowledge("pesoCofre_Agente(0,0)");
		this.addKnowledge("valorAcumulado_Agente(0,0)");
		 
    }

	// Agregado.. lo voy a usar para las acciones
	private int getPesoCofre_Agente() {
		String energyQuery = "pesoCofre_Agente(Peso_Ag," + this.getSituation()
				+ ")";

		Hashtable[] result = this.query(energyQuery);

		int energy = Integer.parseInt(result[0].get("Peso_Ag").toString());

		return energy;
	}

   //Agregado.. lo voy a usar para las acciones
   private int getValorAcumulado_Agente() {
        String energyQuery = "valorAcumulado_Agente(valorAcumulado," + this.getSituation() + ")";

        Hashtable[] result = this.query(energyQuery);

        int energy = Integer.parseInt(result[0].get("valorAcumulado").toString());

        return energy;
    }

	public void setPosicion(Aldea aldea) {
		this.posicion= aldea;
	}
	
	public Aldea getPosicion() {
		
		return posicion;
	}
	
	public int getValor_Acumulado() {
		return valor_Acumulado;
	}
	
	public void setValor_Acumulado(int valor_Acumulado) {
		this.valor_Acumulado = valor_Acumulado;
	}
	
	public void setPesoCofre_Agente(double pesoCofre_Agente) {
		this.pesoCofre_Agente = pesoCofre_Agente;
	}
	
	public double getPesoCofre(){
		return pesoCofre_Agente;
	}
    
}
